﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Database.Entity;
using ASBCC.Database.Service;
using System.Text;

namespace BEGDC.CC.Web.EZVisit
{
    public partial class Report1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DateTime now = DateTime.Now;
                int firstYear = 2011;
                ddlRequestedDateFromYear.Items.Add(new ListItem(firstYear.ToString(), firstYear.ToString()));
                ddlRequestedDateToYear.Items.Add(new ListItem(firstYear.ToString(), firstYear.ToString()));

                if (now.Year > firstYear)
                {
                    for (int i = firstYear + 1; i <= now.Year; i++)
                    {
                        ddlRequestedDateFromYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                        ddlRequestedDateToYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    }
                }

                

                for (int i = 1; i <= 12; i++)
                {
                    ddlRequestedDateFromMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlRequestedDateToMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

                ddlRequestedDateFromYear.SelectedValue = now.Year.ToString();
                ddlRequestedDateToYear.SelectedValue = now.Year.ToString();

                ddlRequestedDateToMonth.SelectedValue = "12";

                //txtRequestedFrom.Text = now.Year.ToString() + "-01-01";
                //txtRequestedFrom.Text = now.Year.ToString() + "-12-31";
                search();
            }
        }

        protected void search()
        {
            List<string> conds = new List<string>();

//            conds.Add(string.Format(@"( (RequestedDateFrom >= '{0}' and RequestedDateTo <= '{0}') 
//or (RequestedDateFrom >= '{1}' and RequestedDateTo <= '{1}') )", txtRequestedFrom.Text, txtRequestedTo.Text));

            conds.Add(string.Format(@"( (RequestedDateFrom >= '{0}' and RequestedDateTo <= '{1}') 
or (RequestedDateFrom >= '{0}' and RequestedDateTo <= '{1}') )", 
                string.Format("{0}-{1}-01", 
                    ddlRequestedDateFromYear.SelectedValue, ddlRequestedDateFromMonth.SelectedValue),

                DateTime.Parse(string.Format("{0}-{1}-01 23:59:59", 
                    ddlRequestedDateToYear.SelectedValue, ddlRequestedDateToMonth.SelectedValue)).AddMonths(1).AddDays(-1).ToString() 
            ));

            if (ddlStatus.SelectedValue.ToLower() != "all")
            {
                conds.Add(string.Format("Status = '{0}'", ddlStatus.SelectedValue));
            }

            EZVisitApplicationTableEntitySet set1 = conds.Count > 0 ?
                EZVisitApplicationManager.SelectByCondition(string.Join(" and ", conds.ToArray())) :
                EZVisitApplicationManager.SelectAll();

            StringBuilder op = new StringBuilder();
            op.Append("<table>");
            foreach (EZVisitApplicationTableEntity ent in set1)
            {
                op.Append(string.Format("<tr><td>{0}</td><td>{1} to {2}</td></tr>", 
                    ent.AppNo, ent.RequestedDateFrom, ent.RequestedDateTo));
            }
            op.Append("</table>");

            lblOutput.Text = op.ToString();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            search();
        }
    }
}
